<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="库位编码">
        <el-input v-model="binL.binL_Code" placeholder="请输入库位编码" />
      </el-form-item>

      <el-form-item label="库位数量">
        <el-input v-model="binL.binL_Nums" placeholder="请输入库位数量" />
      </el-form-item>

      <el-form-item label="当前状态" style="width: 20%">
  <el-select v-model="binL.binL_Status" placeholder="请选择当前状态" clearable>
    <el-option label="空闲" value="0" />
    <el-option label="锁定" value="1" />
  </el-select>
</el-form-item>
      <el-button type="primary" @click="getTableData">查询</el-button>
    </el-form>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="50" />
      <el-table-column prop="binL_Code" label="库位编码" width="120" />

      <el-table-column prop="binL_Type" label="库位类型" width="120">
        <template v-slot="scope">
          <el-text>{{ getBinLType(scope.row.binL_Type) }}</el-text>
        </template>
      </el-table-column>

      <el-table-column prop="binL_UserType" label="库位使用方式" width="120" />
      <el-table-column prop="binL_GoodsCode" label="商品编码" width="120" />
      <el-table-column prop="binL_Width" label="库位宽度" width="120" />
      <el-table-column prop="binL_Height" label="库位高度" width="120" />
      <el-table-column prop="binL_Length" label="库位长度" width="120" />
      <el-table-column prop="binL_Volume" label="库位体积" width="120" />
      <el-table-column prop="binL_Nums" label="库位数量" width="120" />
      <el-table-column prop="binL_UseNums" label="库位已用数量" width="120" />
      <el-table-column prop="binL_Weight" label="库位总重量" width="120" />
      <el-table-column prop="binL_MinNums" label="库位下限容量" width="120" />

      <el-table-column prop="binL_Status" label="库位状态" width="120">
        <template v-slot="scope">
          <el-tag :type="getBinLStatusType(scope.row.binL_Status)">
            {{ getBinLStatus(scope.row.binL_Status) }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="binL_Description" label="库位描述" width="120" />
      <el-table-column prop="address" label="操作">
        <template v-slot="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-model:current-page="binL.pageIndex" v-model:page-size="binL.pageSize" :page-sizes="[10, 20, 30]"
      :background="true" layout="total, sizes, prev, pager, next, jumper" :total="binL.totalCount"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
  </div>
  <div>
    <el-dialog v-model="logic.isOpan" title="编辑库位" width="500">
    <el-form :model="form">
      <el-form-item label="库位编码">
        <el-input v-model="form.binL_Code"/>
      </el-form-item>
      <el-form-item label="库位类型">
        <el-input v-model="form.binL_Type"/>
      </el-form-item>
      <el-form-item label="库位使用方式">
        <el-input v-model="form.binL_UserType"/>
      </el-form-item>
      <el-form-item label="商品编码">
        <el-input v-model="form.binL_GoodsCode"/>
      </el-form-item>
      <el-form-item label="库位宽度">
        <el-input v-model="form.binL_Width"/>
      </el-form-item>
      <el-form-item label="库位高度">
        <el-input v-model="form.binL_Height"/>
      </el-form-item>
      <el-form-item label="库位长度">
        <el-input v-model="form.binL_Length"/>
      </el-form-item>
      <el-form-item label="库位体积">
        <el-input v-model="form.binL_Volume"/>
      </el-form-item>
      <el-form-item label="库位数量">
        <el-input v-model="form.binL_Nums"/>
      </el-form-item>
      <el-form-item label="库位以用数量">
        <el-input v-model="form.binL_UseNums"/>
      </el-form-item>
      <el-form-item label="库位总重量">
        <el-input v-model="form.binL_Weight"/>
      </el-form-item>
      <el-form-item label="库位下限容量">
        <el-input v-model="form.binL_MinNums"/>
      </el-form-item>
      <el-form-item label="库位状态">
        <el-input v-model="form.binL_Status"/>
      </el-form-item>
      <el-form-item label="库位描述">
        <el-input v-model="form.binL_Description"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="logic.isOpan = false">取消</el-button>
        <el-button type="primary" @click="handleSave">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { ElMessage,ElMessageBox } from 'element-plus';

  //逻辑
  const logic =reactive({
      isOpan:false,
      Add:true,
})

const form = reactive({
  binL_Id: '',
  binL_Code: '',
  binL_Type: '',
  binL_UserType: '',
  binL_GoodsCode: '',
  binL_Width: '',
  binL_Height: '',
  binL_Length: '',
  binL_Volume: '',
  binL_Nums: '',
  binL_UseNums:'',
  binL_Weight:'' ,
  binL_MinNums: '',
  binL_Status: '',
  binL_Description: '',
})

const handleEdit = (row: any) => {
  logic.isOpan = true
  form.binL_Id = row.binL_Id
  form.binL_Code = row.binL_Code
  form.binL_Type = row.binL_Type
  form.binL_UserType = row.binL_UserType
  form.binL_GoodsCode = row.binL_GoodsCode
  form.binL_Width = row.binL_Width
  form.binL_Height = row.binL_Height
  form.binL_Length = row.binL_Length
  form.binL_Volume = row.binL_Volume
  form.binL_Nums = row.binL_Nums
  form.binL_UseNums = row.binL_UseNums
  form.binL_Weight = row.binL_Weight
  form.binL_MinNums = row.binL_MinNums
  form.binL_Status = row.binL_Status
  form.binL_Description = row.binL_Description
}
const handleSave = () => {
  if (logic.Add) {
    axios.post('http://localhost:5259/api/BinLocation/UpdateBinLocation', form)
     .then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("修改成功")
          logic.isOpan = false
          getTableData()
        }
        else {
          ElMessage.error(res.data.message)
        }
      })
     .catch((error) => {
        console.error('编辑库位失败:', error);
        ElMessage.error('编辑库位失败，请稍后重试');
      });
  }
}


const tableData = ref([]);
const binL = reactive({
  binL_Code: '',
  binL_Nums: '',
  binL_Status: '',
  binL_UserType: null,
  totalCount: 0,
  totalPage: 0,
  pageIndex: 1,
  pageSize: 10,
});

const getTableData = () => {
  axios.get('http://localhost:5115/api/BinLocation/GetBinLocation', {
    params: {
      binL_Code: binL.binL_Code,
      binL_Nums: binL.binL_Nums,
      binL_Status: binL.binL_Status,
      binL_UserType: binL.binL_UserType,
      pageIndex: binL.pageIndex,
      pageSize: binL.pageSize,
    },
  })
    .then((res) => {
      tableData.value = res.data.data.data;
      binL.totalCount = res.data.data.totalCount;
      binL.totalPage = res.data.data.totalPage;
    })
    .catch((error) => {
      console.error('获取库位数据失败:', error);
      ElMessage.error('获取库位数据失败，请稍后重试');
    });
};

const getBinLType = (type: number) => type === 0 ? '库位' : '货架';

const getBinLStatus = (status: number) => status === 0 ? '空闲' : '锁定';

const getBinLStatusType = (status: number) => status === 0 ? 'success' : 'warning';

onMounted(() => {
  getTableData();
});
const handleSizeChange = (val: number) => {
  binL.pageSize = val
  getTableData()

}
const handleCurrentChange = (val: number) => {
  binL.pageIndex = val
  getTableData()
}

//删除库位
const handleDelete = (row: any) => {
  ElMessageBox.confirm(
    '确定删除吗?',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    axios.delete("http://localhost:5259/api/BinLocation/DelBinLocation?binL_Id=" + row.binL_Id).then(res => {
      if (res.data.code == 200) {
        ElMessage.success("删除成功")
        getTableData()
      }
      else {
        ElMessage.error(res.data.message)
      }
    })
  }).catch(() => {
      ElMessage({
        type: 'info',
        message: '取消操作',
      })
    })
}



</script>
